-
Notifications
You must be signed in to change notification settings - Fork 575
[detailed][benchmark] all_to_all_single with async_op #3436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Contributor
|
@TroyGarden has exported this pull request. If you are a Meta employee, you can view the originating Diff in D83924526. |
c0e2458 to
7444714
Compare
TroyGarden
added a commit
to TroyGarden/torchrec
that referenced
this pull request
Oct 6, 2025
Summary: # context * add benchmark for `all_to_all_single` with `async_op` option. * the comms uses a different cuda stream (comms stream) so it's non-blocking for the followed operations (on main cuda stream) * of course the comms results (pre-allocated output) are not valid until the comms' done (the pre-check fails) * when there's data dependency on the comms' output, user's need to call `req.wait()` explicitly, so that the main cuda stream wait on the comms stream NOTE: the `req.wait()` call is non-blocking on the CPU side. # ref * [torch.distributed](https://docs.pytorch.org/tutorials/beginner/dist_overview.html) * [sync and async comms](https://docs.pytorch.org/docs/stable/distributed.html#collective-functions) * [CUDA semantics](https://docs.pytorch.org/docs/stable/notes/cuda.html) Differential Revision: D83924526
7444714 to
80525b3
Compare
TroyGarden
added a commit
to TroyGarden/torchrec
that referenced
this pull request
Oct 6, 2025
Summary: # context * add benchmark for `all_to_all_single` with `async_op` option. * the comms uses a different cuda stream (comms stream) so it's non-blocking for the followed operations (on main cuda stream) * of course the comms results (pre-allocated output) are not valid until the comms' done (the pre-check fails) * when there's data dependency on the comms' output, user's need to call `req.wait()` explicitly, so that the main cuda stream wait on the comms stream NOTE: the `req.wait()` call is non-blocking on the CPU side. # ref * [torch.distributed](https://docs.pytorch.org/tutorials/beginner/dist_overview.html) * [sync and async comms](https://docs.pytorch.org/docs/stable/distributed.html#collective-functions) * [CUDA semantics](https://docs.pytorch.org/docs/stable/notes/cuda.html) Differential Revision: D83924526
80525b3 to
b991118
Compare
b991118 to
8d3c365
Compare
TroyGarden
added a commit
to TroyGarden/torchrec
that referenced
this pull request
Oct 6, 2025
Summary: # context * add benchmark for `all_to_all_single` with `async_op` option. * the comms uses a different cuda stream (comms stream) so it's non-blocking for the followed operations (on main cuda stream) * of course the comms results (pre-allocated output) are not valid until the comms' done (the pre-check fails) * when there's data dependency on the comms' output, user's need to call `req.wait()` explicitly, so that the main cuda stream wait on the comms stream NOTE: the `req.wait()` call is non-blocking on the CPU side. # ref * [torch.distributed](https://docs.pytorch.org/tutorials/beginner/dist_overview.html) * [sync and async comms](https://docs.pytorch.org/docs/stable/distributed.html#collective-functions) * [CUDA semantics](https://docs.pytorch.org/docs/stable/notes/cuda.html) Differential Revision: D83924526
8d3c365 to
fc28921
Compare
TroyGarden
added a commit
to TroyGarden/torchrec
that referenced
this pull request
Oct 7, 2025
Summary: # context * add benchmark for `all_to_all_single` with `async_op` option. * the comms uses a different cuda stream (comms stream) so it's non-blocking for the followed operations (on main cuda stream) * of course the comms results (pre-allocated output) are not valid until the comms' done (the pre-check fails) * when there's data dependency on the comms' output, user's need to call `req.wait()` explicitly, so that the main cuda stream wait on the comms stream NOTE: the `req.wait()` call is non-blocking on the CPU side. # ref * [torch.distributed](https://docs.pytorch.org/tutorials/beginner/dist_overview.html) * [sync and async comms](https://docs.pytorch.org/docs/stable/distributed.html#collective-functions) * [CUDA semantics](https://docs.pytorch.org/docs/stable/notes/cuda.html) Reviewed By: spmex Differential Revision: D83924526
fc28921 to
2e7f3aa
Compare
TroyGarden
added a commit
to TroyGarden/torchrec
that referenced
this pull request
Oct 7, 2025
Summary: # context * add benchmark for `all_to_all_single` with `async_op` option. * the comms uses a different cuda stream (comms stream) so it's non-blocking for the followed operations (on main cuda stream) * of course the comms results (pre-allocated output) are not valid until the comms' done (the pre-check fails) * when there's data dependency on the comms' output, user's need to call `req.wait()` explicitly, so that the main cuda stream wait on the comms stream NOTE: the `req.wait()` call is non-blocking on the CPU side. # ref * [torch.distributed](https://docs.pytorch.org/tutorials/beginner/dist_overview.html) * [sync and async comms](https://docs.pytorch.org/docs/stable/distributed.html#collective-functions) * [CUDA semantics](https://docs.pytorch.org/docs/stable/notes/cuda.html) # optimization * the data validation is done on the device side, and very often the validation result is needed from the cpu side * in a previously example (a2a-sync), the assertion needs the `checks` on the cpu side, so it's blocking the cpu execution (see below) {F1982527184} * actually the `checks` is available at the gpu side once the "comms validation" is done, and a device-to-host data transfer can be initiated. however, this device-to-host data transfer is blocking the following cpu execution (i.e., "irrelevant compute") {F1982527242} * we tried to use `non_blocking=True` in the copy_to, and the results are very interesting: all the cpu executions are done very early, this is because the cpu has no idea about the completeness of non-blocking device-to-host data transfer, it will just go ahead executing, which of coruse causes assertion error (changed to `print(checks)` to bypass the assertion in order to generate the trace) {F1982527281} * the proper way of doing this is to use cuda.event as in this diff, so that the assert only needs to wait until the data (`checks`) becomes available on the cpu side. {F1982527334} * as for the async all_to_all_single case, the 2nd batch (CPU) starts right after the 1st assertion, which is done right after the device-to-host data transfer. {F1982527765} # results * [a2a sync trace](https://drive.google.com/file/d/1xI-qlI7V6zbmcbuQGyZgqFC3ZMY1scro/view?usp=sharing) * [a2a async trace](https://drive.google.com/file/d/1eboZ01quSZjKBtBcdu4vXX9zyokzohqi/view?usp=sharing) Reviewed By: spmex Differential Revision: D83924526
2e7f3aa to
534c7e7
Compare
TroyGarden
added a commit
to TroyGarden/torchrec
that referenced
this pull request
Oct 7, 2025
Summary: # context * add benchmark for `all_to_all_single` with `async_op` option. * the comms uses a different cuda stream (comms stream) so it's non-blocking for the followed operations (on main cuda stream) * of course the comms results (pre-allocated output) are not valid until the comms' done (the pre-check fails) * when there's data dependency on the comms' output, user's need to call `req.wait()` explicitly, so that the main cuda stream wait on the comms stream NOTE: the `req.wait()` call is non-blocking on the CPU side. # ref * [torch.distributed](https://docs.pytorch.org/tutorials/beginner/dist_overview.html) * [sync and async comms](https://docs.pytorch.org/docs/stable/distributed.html#collective-functions) * [CUDA semantics](https://docs.pytorch.org/docs/stable/notes/cuda.html) # optimization * the data validation is done on the device side, and very often the validation result is needed from the cpu side * in a previously example (a2a-sync), the assertion needs the `checks` on the cpu side, so it's blocking the cpu execution (see below) {F1982527184} * actually the `checks` is available at the gpu side once the "comms validation" is done, and a device-to-host data transfer can be initiated. however, this device-to-host data transfer is blocking the following cpu execution (i.e., "irrelevant compute") {F1982527242} * we tried to use `non_blocking=True` in the copy_to, and the results are very interesting: all the cpu executions are done very early, this is because the cpu has no idea about the completeness of non-blocking device-to-host data transfer, it will just go ahead executing, which of coruse causes assertion error (changed to `print(checks)` to bypass the assertion in order to generate the trace) {F1982527281} * the proper way of doing this is to use cuda.event as in this diff, so that the assert only needs to wait until the data (`checks`) becomes available on the cpu side. {F1982527334} * as for the async all_to_all_single case, the 2nd batch (CPU) starts right after the 1st assertion, which is done right after the device-to-host data transfer. {F1982527765} # results * [a2a sync trace](https://drive.google.com/file/d/1xI-qlI7V6zbmcbuQGyZgqFC3ZMY1scro/view?usp=sharing) * [a2a async trace](https://drive.google.com/file/d/1eboZ01quSZjKBtBcdu4vXX9zyokzohqi/view?usp=sharing) Reviewed By: spmex Differential Revision: D83924526
967605e to
a8177d1
Compare
TroyGarden
added a commit
to TroyGarden/torchrec
that referenced
this pull request
Oct 7, 2025
Summary: # context * add benchmark for `all_to_all_single` with `async_op` option. * the comms uses a different cuda stream (comms stream) so it's non-blocking for the followed operations (on main cuda stream) * of course the comms results (pre-allocated output) are not valid until the comms' done (the pre-check fails) * when there's data dependency on the comms' output, user's need to call `req.wait()` explicitly, so that the main cuda stream wait on the comms stream NOTE: the `req.wait()` call is non-blocking on the CPU side. # ref * [torch.distributed](https://docs.pytorch.org/tutorials/beginner/dist_overview.html) * [sync and async comms](https://docs.pytorch.org/docs/stable/distributed.html#collective-functions) * [CUDA semantics](https://docs.pytorch.org/docs/stable/notes/cuda.html) # optimization * the data validation is done on the device side, and very often the validation result is needed from the cpu side * in a previously example (a2a-sync), the assertion needs the `checks` on the cpu side, so it's blocking the cpu execution (see below) {F1982527184} * actually the `checks` is available at the gpu side once the "comms validation" is done, and a device-to-host data transfer can be initiated. however, this device-to-host data transfer is blocking the following cpu execution (i.e., "irrelevant compute") {F1982527242} * we tried to use `non_blocking=True` in the copy_to, and the results are very interesting: all the cpu executions are done very early, this is because the cpu has no idea about the completeness of non-blocking device-to-host data transfer, it will just go ahead executing, which of coruse causes assertion error (changed to `print(checks)` to bypass the assertion in order to generate the trace) {F1982527281} * the proper way of doing this is to use cuda.event as in this diff, so that the assert only needs to wait until the data (`checks`) becomes available on the cpu side. {F1982527334} * as for the async all_to_all_single case, the 2nd batch (CPU) starts right after the 1st assertion, which is done right after the device-to-host data transfer. {F1982527765} # results * [a2a sync trace](https://drive.google.com/file/d/1xI-qlI7V6zbmcbuQGyZgqFC3ZMY1scro/view?usp=sharing) * [a2a async trace](https://drive.google.com/file/d/1eboZ01quSZjKBtBcdu4vXX9zyokzohqi/view?usp=sharing) Reviewed By: spmex Differential Revision: D83924526
TroyGarden
added a commit
to TroyGarden/torchrec
that referenced
this pull request
Oct 7, 2025
Summary: # context * add benchmark for `all_to_all_single` with `async_op` option. * the comms uses a different cuda stream (comms stream) so it's non-blocking for the followed operations (on main cuda stream) * of course the comms results (pre-allocated output) are not valid until the comms' done (the pre-check fails) * when there's data dependency on the comms' output, user's need to call `req.wait()` explicitly, so that the main cuda stream wait on the comms stream NOTE: the `req.wait()` call is non-blocking on the CPU side. # ref * [torch.distributed](https://docs.pytorch.org/tutorials/beginner/dist_overview.html) * [sync and async comms](https://docs.pytorch.org/docs/stable/distributed.html#collective-functions) * [CUDA semantics](https://docs.pytorch.org/docs/stable/notes/cuda.html) # optimization * the data validation is done on the device side, and very often the validation result is needed from the cpu side * in a previously example (a2a-sync), the assertion needs the `checks` on the cpu side, so it's blocking the cpu execution (see below) {F1982527184} * actually the `checks` is available at the gpu side once the "comms validation" is done, and a device-to-host data transfer can be initiated. however, this device-to-host data transfer is blocking the following cpu execution (i.e., "irrelevant compute") {F1982527242} * we tried to use `non_blocking=True` in the copy_to, and the results are very interesting: all the cpu executions are done very early, this is because the cpu has no idea about the completeness of non-blocking device-to-host data transfer, it will just go ahead executing, which of coruse causes assertion error (changed to `print(checks)` to bypass the assertion in order to generate the trace) {F1982527281} * the proper way of doing this is to use cuda.event as in this diff, so that the assert only needs to wait until the data (`checks`) becomes available on the cpu side. {F1982527334} * as for the async all_to_all_single case, the 2nd batch (CPU) starts right after the 1st assertion, which is done right after the device-to-host data transfer. {F1982527765} # results * [a2a sync trace](https://drive.google.com/file/d/1xI-qlI7V6zbmcbuQGyZgqFC3ZMY1scro/view?usp=sharing) * [a2a async trace](https://drive.google.com/file/d/1eboZ01quSZjKBtBcdu4vXX9zyokzohqi/view?usp=sharing) Reviewed By: spmex Differential Revision: D83924526
a8177d1 to
dd43f81
Compare
Summary: Pull Request resolved: meta-pytorch#3436 # context * add benchmark for `all_to_all_single` with `async_op` option. * the comms uses a different cuda stream (comms stream) so it's non-blocking for the followed operations (on main cuda stream) * of course the comms results (pre-allocated output) are not valid until the comms' done (the pre-check fails) * when there's data dependency on the comms' output, user's need to call `req.wait()` explicitly, so that the main cuda stream wait on the comms stream NOTE: the `req.wait()` call is non-blocking on the CPU side. # ref * [torch.distributed](https://docs.pytorch.org/tutorials/beginner/dist_overview.html) * [sync and async comms](https://docs.pytorch.org/docs/stable/distributed.html#collective-functions) * [CUDA semantics](https://docs.pytorch.org/docs/stable/notes/cuda.html) # optimization * the data validation is done on the device side, and very often the validation result is needed from the cpu side * in a previously example (a2a-sync), the assertion needs the `checks` on the cpu side, so it's blocking the cpu execution (see below) {F1982527184} * actually the `checks` is available at the gpu side once the "comms validation" is done, and a device-to-host data transfer can be initiated. however, this device-to-host data transfer is blocking the following cpu execution (i.e., "irrelevant compute") {F1982527242} * we tried to use `non_blocking=True` in the copy_to, and the results are very interesting: all the cpu executions are done very early, this is because the cpu has no idea about the completeness of non-blocking device-to-host data transfer, it will just go ahead executing, which of coruse causes assertion error (changed to `print(checks)` to bypass the assertion in order to generate the trace) {F1982527281} * the proper way of doing this is to use cuda.event as in this diff, so that the assert only needs to wait until the data (`checks`) becomes available on the cpu side. {F1982527334} * as for the async all_to_all_single case, the 2nd batch (CPU) starts right after the 1st assertion, which is done right after the device-to-host data transfer. {F1982527765} # results * [a2a sync trace](https://drive.google.com/file/d/1xI-qlI7V6zbmcbuQGyZgqFC3ZMY1scro/view?usp=sharing) * [a2a async trace](https://drive.google.com/file/d/1eboZ01quSZjKBtBcdu4vXX9zyokzohqi/view?usp=sharing) Reviewed By: spmex Differential Revision: D83924526
dd43f81 to
463971e
Compare
TroyGarden
added a commit
to TroyGarden/torchrec
that referenced
this pull request
Oct 22, 2025
Summary: # TL;DR * A new `DeviceToHostTensorAwaitable` class is available to wrap the device-to-host data transfer, and defers the `cudaEventSync` call until the data is really used on the host. * It aims at helping sync-point removal in training optimization which often suffers from cpu-blocking sync points. # why awaitable * as shown in the following diagram, a comms op is often better to overlap with another (irrelevant) compute op to better utilize the device capability * the idea is to **defer** the `wait()` call until running the function that uses the result from the comm op * a convenient way to achieve this "deferring" behavior is to use the `lazy_awaitable` concept, which is already [implemented in torchrec](https://github.com/meta-pytorch/torchrec/blob/main/torchrec/distributed/types.py#L368) * diagram of (lazy_)awaitable in torchrec {F1982900178} # why device-to-host transfer * there are scenarios that the on-device data is needed from the host side, such as metrics logging and data-dependent shape operation. * those pattern creates a device-to-host sync (data transfer) that often blocks the cpu execution, and the correct implementation (with `.to(non_blocking=True)` and cuda event: [PR 3436](meta-pytorch#3436)) usually spans across multiple code domain making it difficult to optimize. * here we borrow the `LazyAwaitable` concept for the device-side comms and wrap the (1) non-blocking device-to-host data transfer, and (2) `cuda_event.wait()` inside a `DeviceToHostTensorAwaitable` class for better user experience. * diagram of lazy_awaitable for device-to-host data transfer {F1982900233} # results * the "comms check" result is on device and is needed for validation (host-side assertion) * the `DeviceToHostTensorAwaitable.wait()` **defer** the cudaEventSync until the very end where the result is really needed by host. * You can see the post-comms computes are scheduled before the assertion on the host side. {F1982900468} NOTE: in this version of implementation we don't use a separate stream (as shown in the diagram above) for the non-blocking device-to-host data transfer because usually the data volume is relatively small. Differential Revision: D85211205
meta-codesync bot
pushed a commit
that referenced
this pull request
Oct 22, 2025
Summary: Pull Request resolved: #3477 workplace post: https://fb.workplace.com/groups/811751593969209/permalink/1285164823294548/ # TL;DR * A new `DeviceToHostTensorAwaitable` class is available to wrap the device-to-host data transfer, and defers the `cudaEventSync` call until the data is really used on the host. * It aims at helping sync-point removal in training optimization which often suffers from cpu-blocking sync points. # why awaitable * as shown in the following diagram, a comms op is often better to overlap with another (irrelevant) compute op to better utilize the device capability * the idea is to **defer** the `wait()` call until running the function that uses the result from the comm op * a convenient way to achieve this "deferring" behavior is to use the `lazy_awaitable` concept, which is already [implemented in torchrec](https://github.com/meta-pytorch/torchrec/blob/main/torchrec/distributed/types.py#L368) * diagram of (lazy_)awaitable in torchrec {F1982900178} # why device-to-host transfer * there are scenarios that the on-device data is needed from the host side, such as metrics logging and data-dependent shape operation. * those pattern creates a device-to-host sync (data transfer) that often blocks the cpu execution, and the correct implementation (with `.to(non_blocking=True)` and cuda event: [PR 3436](#3436)) usually spans across multiple code domain making it difficult to optimize. * here we borrow the `LazyAwaitable` concept for the device-side comms and wrap the (1) non-blocking device-to-host data transfer, and (2) `cuda_event.wait()` inside a `DeviceToHostTensorAwaitable` class for better user experience. * diagram of lazy_awaitable for device-to-host data transfer {F1982900233} # results * the "comms check" result is on device and is needed for validation (host-side assertion) * the `DeviceToHostTensorAwaitable.wait()` **defer** the cudaEventSync until the very end where the result is really needed by host. * You can see the post-comms computes are scheduled before the assertion on the host side. {F1982900468} NOTE: in this version of implementation we don't use a separate stream (as shown in the diagram above) for the non-blocking device-to-host data transfer because usually the data volume is relatively small. {F1982901286} Reviewed By: spmex Differential Revision: D85211205 fbshipit-source-id: 41d03230dd9b190085545cfb76192d59375646c4
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
CLA Signed
This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
fb-exported
meta-exported
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary:
context
all_to_all_singlewithasync_opoption.req.wait()explicitly, so that the main cuda stream wait on the comms streamref
optimization
checkson the cpu side, so it's blocking the cpu execution (see below)checksis available at the gpu side once the "comms validation" is done, and a device-to-host data transfer can be initiated. however, this device-to-host data transfer is blocking the following cpu execution (i.e., "irrelevant compute")non_blocking=Truein the copy_to, and the results are very interesting:all the cpu executions are done very early, this is because the cpu has no idea about the completeness of non-blocking device-to-host data transfer, it will just go ahead executing, which of coruse causes assertion error (changed to
print(checks)to bypass the assertion in order to generate the trace)checks) becomes available on the cpu side.results
Differential Revision: D83924526